/*
 * @Author: silence
 * @Date: 2023-03-15 09:23:02
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-04-15 13:20:06
 * @Description: 我的页面-顶部-用户信息
 */
import 'package:ems_neutral_app/common/module/WebViewPage/WebViewPage.dart';
import 'package:ems_neutral_app/main.dart';
import 'package:ems_neutral_app/pages/Mine/AccountManager/AccountManagerPage.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/router/ARouter.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../../utils/color/ColorUtils.dart';

import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../../utils/eventBus/eventBus.dart';
import '../../../../utils/string/StringConstant.dart';
import '../../../PowerStation/Manager/PowerStationManagerPage.dart';
import '../../AccountManager/Controller/AccountManagerController.dart';
import '../../Fault/FaultPage.dart';

class MinePageHeader extends StatefulWidget {
  const MinePageHeader({super.key});

  @override
  State<MinePageHeader> createState() => _MinePageHeaderState();
}

class _MinePageHeaderState extends State<MinePageHeader> {
  AccountManagerController _accountManagerController =
      Get.put(AccountManagerController());
  @override
  Widget build(BuildContext context) {
    return mainWidget();
  }

  @override
  void initState() {
    super.initState();

    if (this.mounted) {
      bus.on(StringConstant.UserLoginInEvent, (value) {
        _accountManagerController.requestUserInfo(
            context: navigatorKey.currentState!.context);
      });
    }
  }

  mainWidget() {
    return Container(
      margin: EdgeInsets.only(top: 50.w, bottom: 25.w),
      padding: EdgeInsets.symmetric(horizontal: 20.w),
      child: Column(
        children: [
          Obx(() => titleView()),
          categoryView(),
        ],
      ),
    );
  }

  titleView() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SilImage(
          _accountManagerController.avatar.value,
          // 'imgs/personal/personal_photo_defaultavatar_75@3x.png',
          height: 65.w,
          width: 65.w,
          borderColor: Colors.white,
          radius: 32.5.w,
          borderWidth: 0.5.w,
        ),
        Expanded(
            child: SilText(
          _accountManagerController.nickName.value,
          margin: EdgeInsets.symmetric(horizontal: 10.w),
          fontSize: 18.w,
          textAlign: TextAlign.center,
          color: ColorUtils.color_font_main,
        )),
        SilButton(
          imgPath: 'imgs/personal/personal_sz.png',
          imgHeight: 20.w,
          imgWidth: 20.w,
          onTapFunc: () => ARoute.gotoPage(context, AccountManagerPage()),
        )
      ],
    );
  }

  categoryView() {
    return Container(
        margin: EdgeInsets.only(top: 30.w, bottom: 20.w),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            categoryItem(
              title: LangOf(context)?.mine?.station_management,
              imgPath: 'imgs/personal/personal_dzgl.png',
              onTapFunc: () {
                ARoute.gotoPage(context, PowerStationManagerPage());
              },
            ),
            categoryItem(
              title: LangOf(context)?.mine?.fault_statistice,
              imgPath: 'imgs/personal/personal_gztj.png',
              onTapFunc: () {
                ARoute.gotoPage(context, FaultPage());
              },
            ),
            categoryItem(
              title: LangOf(context)?.price?.title,
              imgPath: 'imgs/personal/personal_yhph.png',
              onTapFunc: () {
                ARoute.gotoPage(
                    context,
                    WebViewPage(
                      'https://www.nordpoolgroup.com/en/Market-data1/Dayahead/Area-Prices/ALL1/Hourly/?view=table',
                      title: LangOf(context)?.price?.title,
                    ));
              },
            ),
          ],
        ));
  }

  categoryItem({String? title, String? imgPath, void Function()? onTapFunc}) {
    return Expanded(
        child: SilButton(
      useConstrained: true,
      imgPath: imgPath,
      imgHeight: 37.5.w,
      imgWidth: 35.w,
      imagePosition: ImagePosition.Up,
      space: 3.w,
      title: title,
      titleColor: ColorUtils.color_font_main,
      fontSize: 14.sp,
      onTapFunc: onTapFunc,
      maxLines: 0,
      textAlign: TextAlign.center,
    ));
  }
}
